<?php

namespace app\platform\controller\terminal;

use app\BaseController;

use app\common\logic\DataLogic;

use app\common\model\account\AccountModel;

use app\common\model\account\ShopAccountModel;



class ShopAccount extends BaseController{

    public function index()

    {
        $role_id = $this->request->param('role_id');
        $where=[

            ['shop_id','=',$this->request->shopId],
//            ['is_delete','=',0]

        ];
        if(!empty($role_id)){
            $where[]=['role_id','=',$role_id];
        }else{
            if($this->request->role_id == 2) $where[]=['role_id','=',2];
        }
        $shop_account= new ShopAccountModel();

        $order=['add_time'=>'desc'];

        $field='shop_account_id,account_id,add_time';

        $append=['add_time_format'];

        $shop_account=$shop_account->with(['account'=>function($query){

            $query->field('account_id,username,mobile,face,number,shop_password,status');

        }]);

        $return=DataLogic::getDataList($shop_account,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }



    public function show()

    {

        $shop_account_id = $this->request->param('shop_account_id/d',0);

        if($shop_account_id==0){

            return $this->success([]);

        }



        if(null === $shop_account = ShopAccountModel::find($shop_account_id)){

            return $this->error('帐号不见了');

        }

        if($shop_account->shop_id != $this->request->shopId){

            return $this->error('这个商铺没有这个账号');

        }

        if($this->request->role_id==2 && $shop_account->role_id==1){

            return $this->error('您没有权限修改这个账号');

        }

        if(null === $account = AccountModel::find($shop_account->account_id)){

            return $this->error('账号不存在');

        }

        if($account->is_delete != 0){

            return $this->error('账号不存在');

        }

        $detail=[

            'username'=>$account->username,
            'account_id'=>$account->account_id,
            'shop_password'=>$account->shop_password,
            'status'=>$account->status,
            'shop_account_id'=>$shop_account_id,

            'mobile'=>$account->mobile,
            'number'=>$account->number,
            'face'=>$account->face

        ];

        return $this->success(['detail'=>$detail]);

    }



    public function save()
    {
        $username=$this->request->param('username');
        $mobile=$this->request->param('mobile');
        $shop_password=$this->request->param('shop_password');
        $face=$this->request->param('face');
        $role_id=$this->request->param('role_id');
        $account_id=$this->request->param('account_id');
        if(empty($role_id)){
            $role_id=2;
        }
        if(empty($username) || mb_strlen($username) >20){

            return $this->error('用户名需要填写且长度不能大于20');

        }

        if(empty($mobile) || !is_mobile($mobile)){

            return $this->error('请填写正确的手机号');

        }

       

//        if(null === $account = AccountModel::where([['mobile','=',$mobile],['is_delete','=',0]])->find()){

            $account = new AccountModel();

            $account->mobile=$mobile;

            $account->username=$username;
            $account->face=$face;
            if($role_id==3){//收银账号
                $account->number=$account->getnumber();
            }else{
                $account->number=date("YmdHis").rand(111111,999999);
            }
            if(!empty($shop_password))  $account->shop_password=md5($shop_password);
            if(!empty($account_id))  $account->account_id=$account_id;

            $account->save();

//        }
        if(null !== ShopAccountModel::where([
                ['shop_id','=',$this->request->shopId],
                ['account_id','=',$account->account_id]
            ])->find() && empty($account_id)){
            return $this->error('这个帐号已经与这个商铺绑定过了');
        }

     

        $shop_account=new ShopAccountModel();

        $shop_account->shop_id=$this->request->shopId;

        $shop_account->role_id=$role_id;

        $shop_account->account_id=$account->account_id;

        $shop_account->save();

        return $this->success('操作成功');

    }
    public function edit()
    {
        $username=$this->request->param('username');
        $mobile=$this->request->param('mobile');
        $shop_password=$this->request->param('shop_password');
        $face=$this->request->param('face');
        $account_id=$this->request->param('account_id');
        if(empty($account_id)){
            return $this->error('账号id不能为空');
        }
        if(empty($username) || mb_strlen($username) >20){
            return $this->error('用户名需要填写且长度不能大于20');
        }
        if(empty($mobile) || !is_mobile($mobile)){
            return $this->error('请填写正确的手机号');
        }
        $account = new AccountModel();
        $s=[];
        $s['account_id']=$account_id;
        $up['mobile']=$mobile;
        $up['username']=$username;
        $up['face']=$face;
        if(!empty($shop_password))  $up['shop_password']=md5($shop_password);
        $account->update($up,$s);
        return $this->success('操作成功');

    }
    /*修改密码*/
    public function saveShopPassword(){
        $shop_password=$this->request->param('shop_password');
        if(empty($shop_password))  return $this->error('密码不能为空');
        AccountModel::where(['account_id'=>$this->request->accountId])->save(['shop_password'=>md5($shop_password)]);
        return $this->success('操作成功');
    }

    public function del()

    {

        $shop_account_id = $this->request->param('shop_account_id/d',0);



        if(null === $shop_account = ShopAccountModel::find($shop_account_id)){

            return $this->error('帐号不见了');

        }

        if($shop_account->shop_id != $this->request->shopId){

            return $this->error('这个商铺没有这个账号');

        }

        if($shop_account->role_id==1){

            return $this->error('创始人无法解绑');

        }

        if($this->request->role_id==2 && $shop_account->role_id==1){

            return $this->error('您没有权限解绑这个账号');

        }

        if(null === $account = AccountModel::find($shop_account->account_id)){

            return $this->error('账号不存在');

        }

        if($account->is_delete != 0){

            return $this->error('账号不存在');

        }
//        AccountModel::where(['account_id'=>$shop_account->account_id])->save(['is_delete'=>1]);
        $shop_account->delete();

        return $this->success('解绑成功');

    }
    public function effect(){
        $shop_account_id = $this->request->param('shop_account_id/d',0);
        if(empty($shop_account_id)){
            return $this->error('ID错误');
        }
        $status=$this->request->param('status',null);
        if($status===null){
            return $this->error('status参数错误');
        }
        if(null === $shop_account = ShopAccountModel::find($shop_account_id)){

            return $this->error('帐号不见了');

        }
        $up['status']=$status;
        AccountModel::where(['account_id'=>$shop_account->account_id])->save($up);
        return $this->success();
    }

}

